﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace Animation_Editor
{
    public partial class MainScreen : Form
    {
        //Rectangle testRect;

        Rectangle RenderRect;
        Rectangle CollisionRect;
        Rectangle ActiveRect;
        Point AnchorPoint;

        List<Frame> listofFrames= new List<Frame>();
        List<AnimationFrames> listofAnimations = new List<AnimationFrames>();

        string currentImageName;
        int bmp = -1;
        int bkg = -1;

        bool _bRendeRect;
        bool _bCollisionRect;
        bool _bActiveRect;
        bool _bAnchorPoint;
        bool _bZoom;
        bool _bIsThereAnimation;

        bool _bPlay;
        bool _bPause;
        bool _bStop;

        decimal numFrames;

        public bool looping=true;
        //public EventHandler ValueChanged;
        string RelPath;

        public MainScreen()
        {
            InitializeComponent();

            FolderBrowserDialog rp = new FolderBrowserDialog();
            if (DialogResult.OK == rp.ShowDialog())
            {
                RelPath = rp.SelectedPath;
                System.IO.Path.Combine(RelPath);
            }

            ManagedDirect3D.Instance.InitManagedDirect3D(splitContainer2.Panel2, false);
            ManagedDirect3D.Instance.InitManagedDirect3D(AnimPreview, false);
            ManagedTextureManager.Instance.InitManagedTextureManager(ManagedDirect3D.Instance.Device, ManagedDirect3D.Instance.Sprite);

            _bIsThereAnimation = false;
          
            Frame fn = new Frame();
            fn.FrameNumber = 0;
            fn.RRenderRect = new Rectangle();
            fn.RCollisionRect = new Rectangle();
            fn.RActiveRect = new Rectangle();
            fn.PAnchorPoint = new Point(-10,-10);
            fn.SEventString = "";
            fn.DTimer = 0;
            //fn.BIsLooping = false;
            FrameNumber.Items.Add(fn);
            FrameNumber.SelectedItem = fn;
            listofFrames.Add(fn);
            numFrames = 1;
        }

        public void Update()
        {
            //listofFrames[FrameNumber.SelectedIndex] = (Frame)FrameNumber.SelectedItem;
            while (_bPlay&&AnimationFrameNumber.SelectedIndex>=0)
            {
                AnimationFrameNumber.SetSelected(AnimationFrameNumber.SelectedIndex++, true);
                if (AnimationFrameNumber.Items.Count >= AnimationFrameNumber.SelectedIndex)
                    AnimationFrameNumber.SelectedIndex = 0;
            }
        }

        public void Render()
        {
            ManagedDirect3D.Instance.Clear(splitContainer2.Panel2,255, 255, 255);
            ManagedDirect3D.Instance.DeviceBegin();
            ManagedDirect3D.Instance.SpriteBegin();

            if (bmp != -1)
            {
                ManagedTextureManager.Instance.Draw(bmp, 0, 0, 1.0f, 1.0f, Rectangle.Empty, 0, 0, 0, Color.White.ToArgb());
            }

            ManagedDirect3D.Instance.Sprite.Flush();

            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex>=0)
            {
                for (int i = 0; i < listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList.Count; i++)
                {
                    if (listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[i].Equals(listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex]) == true)
                    {
                        DrawRectangle(listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[i].RRenderRect, 255, 0, 0, listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[i].ToString());
                        DrawRectangle(listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[i].RCollisionRect, 0, 0, 255, "");
                        DrawRectangle(listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[i].RActiveRect, 255, 255, 0, "");
                        DrawAnchorPoint(listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[i].PAnchorPoint);
                    }
                    else
                        DrawRectangle(listofFrames[i].RRenderRect, 0, 0, 0, listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[i].ToString());
                }
            }
            else if (FrameNumber.SelectedIndex>=0)
            {
                for (int i = 0; i < listofFrames.Count; i++)
                {
                    if (listofFrames[i].Equals(listofFrames[FrameNumber.SelectedIndex]) == true)
                    {
                        DrawRectangle(listofFrames[i].RRenderRect, 255, 0, 0, listofFrames[i].ToString());
                        DrawRectangle(listofFrames[i].RCollisionRect, 0, 0, 255, "");
                        DrawRectangle(listofFrames[i].RActiveRect, 255, 255, 0, "");
                        DrawAnchorPoint(listofFrames[i].PAnchorPoint);
                    }
                    else
                        DrawRectangle(listofFrames[i].RRenderRect, 0, 0, 0, listofFrames[i].ToString());
                }
            }

            ManagedDirect3D.Instance.SpriteEnd();
            ManagedDirect3D.Instance.DeviceEnd();
            ManagedDirect3D.Instance.Present(splitContainer2.Panel2);



            ManagedDirect3D.Instance.Clear(AnimPreview, 255, 255, 255);
            ManagedDirect3D.Instance.DeviceBegin();
            ManagedDirect3D.Instance.SpriteBegin();

            if (bkg != -1)
            {
                ManagedTextureManager.Instance.Draw(bkg, 0, 0, 1.0f, 1.0f, Rectangle.Empty, 0, 0, 0, Color.White.ToArgb());
            }

            if (bmp != -1 && AnimationList.SelectedIndex>=0 && AnimationFrameNumber.SelectedIndex>=0)
            {
                ManagedTextureManager.Instance.Draw(bmp, 0, 0, 1.0f, 1.0f, new Rectangle(listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.X,
                    listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Y,listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Width,
                    listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Height), 0, 0, 0, Color.White.ToArgb());
            }



            ManagedDirect3D.Instance.SpriteEnd();
            ManagedDirect3D.Instance.DeviceEnd();
            ManagedDirect3D.Instance.Present(AnimPreview);
        }

        private void PreviewRender()
        {

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AnimationList.Items.Count != 0)
                AnimationList.Items.Clear();

            if (AnimationFrameNumber.Items.Count != 0)
                AnimationFrameNumber.Items.Clear();

            if (FrameNumber.Items.Count != 0)
                FrameNumber.Items.Clear();

            AnimName.Clear();
            listofFrames.Clear();
            listofAnimations.Clear();

            FolderBrowserDialog rp = new FolderBrowserDialog();
            if (DialogResult.OK == rp.ShowDialog())
            {
                RelPath = rp.SelectedPath;
                System.IO.Path.Combine(RelPath);
            }

            Frame fn = new Frame();
            fn.FrameNumber = 0;
            fn.RRenderRect = new Rectangle();
            fn.RCollisionRect = new Rectangle();
            fn.RActiveRect = new Rectangle();
            fn.PAnchorPoint = new Point();
            fn.SEventString = "";
            fn.DTimer = 0;
            //fn.BIsLooping = false;
            FrameNumber.Items.Add(fn);
            FrameNumber.SelectedItem = fn;
            listofFrames.Add(fn);
            numFrames = 1;
        }

        private void splitContainer2_Panel2_MouseDown(object sender, MouseEventArgs e)
        {
            //testRect = new Rectangle(e.X, e.Y, 0, 0);

            if (_bRendeRect)
                RenderRect = new Rectangle(e.X, e.Y, 0, 0);
            else if (_bCollisionRect)
                CollisionRect = new Rectangle(e.X, e.Y, 0, 0);
            else if (_bActiveRect)
                ActiveRect = new Rectangle(e.X, e.Y, 0, 0);
            else if (_bAnchorPoint)
                AnchorPoint = new Point(e.X, e.Y);


            //splitContainer2.Panel2.Invalidate();
        }

        private void splitContainer2_Panel2_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //testRect = new Rectangle(testRect.Left, testRect.Top, e.X - testRect.Left, e.Y - testRect.Top);
                if (_bRendeRect)
                    RenderRect = new Rectangle(RenderRect.Left, RenderRect.Top, e.X - RenderRect.Left, e.Y - RenderRect.Top);
                else if (_bCollisionRect)
                    CollisionRect = new Rectangle(CollisionRect.Left, CollisionRect.Top, e.X - CollisionRect.Left, e.Y - CollisionRect.Top);
                else if (_bActiveRect)
                    ActiveRect = new Rectangle(ActiveRect.Left, ActiveRect.Top, e.X - ActiveRect.Left, e.Y - ActiveRect.Top);
                else if (_bAnchorPoint)
                    AnchorPoint = new Point(e.X, e.Y);
            }

            RenderRectXUD.Value = RenderRect.X;
            RenderRectYUD.Value = RenderRect.Y;
            RenderRectWUD.Value = RenderRect.Width;
            RenderRectHUD.Value = RenderRect.Height;

            CollisionRectXUD.Value = CollisionRect.X;
            CollisionRectYUD.Value = CollisionRect.Y;
            CollisionRectWUD.Value = CollisionRect.Width;
            CollisionRectHUD.Value = CollisionRect.Height;

            ActiveRectXUD.Value = ActiveRect.X;
            ActiveRectYUD.Value = ActiveRect.Y;
            ActiveRectWUD.Value = ActiveRect.Width;
            ActiveRectHUD.Value = ActiveRect.Height;

            AnchorPointXUD.Value = AnchorPoint.X;
            AnchorPointYUD.Value = AnchorPoint.Y;

            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect = RenderRect;
                listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect = CollisionRect;
                listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect = ActiveRect;
                listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].PAnchorPoint = AnchorPoint;
            }
            else if (FrameNumber.SelectedIndex>=0)
            {
                listofFrames[FrameNumber.SelectedIndex].RRenderRect = RenderRect;
                listofFrames[FrameNumber.SelectedIndex].RCollisionRect = CollisionRect;
                listofFrames[FrameNumber.SelectedIndex].RActiveRect = ActiveRect;
                listofFrames[FrameNumber.SelectedIndex].PAnchorPoint = AnchorPoint;
            }
        }


        private void RRECTB_Click(object sender, EventArgs e)
        {
            _bRendeRect = true;
            _bCollisionRect = false;
            _bActiveRect = false;
            _bAnchorPoint = false;
            _bZoom = false;
        }

        private void CRECTB_Click(object sender, EventArgs e)
        {
            _bRendeRect = false;
            _bCollisionRect = true;
            _bActiveRect = false;
            _bAnchorPoint = false;
            _bZoom = false;
        }

        private void ARECTB_Click(object sender, EventArgs e)
        {
            _bRendeRect = false;
            _bCollisionRect = false;
            _bActiveRect = true;
            _bAnchorPoint = false;
            _bZoom = false;
        }

        private void APOINTB_Click(object sender, EventArgs e)
        {
            _bRendeRect = false;
            _bCollisionRect = false;
            _bActiveRect = false;
            _bAnchorPoint = true;
            _bZoom = false;
        }

        private void Zoom_Click(object sender, EventArgs e)
        {
            _bRendeRect = false;
            _bCollisionRect = false;
            _bActiveRect = false;
            _bAnchorPoint = false;
            _bZoom = true;
        }

        private void NewFrame_Click(object sender, EventArgs e)
        {
            Frame fn = new Frame();
            fn.FrameNumber = numFrames;
            fn.RRenderRect = new Rectangle();
            fn.RCollisionRect = new Rectangle();
            fn.RActiveRect = new Rectangle();
            fn.PAnchorPoint = new Point(-10,-10);
            fn.SEventString = "";
            fn.DTimer = 0;
            //fn.BIsLooping = false;
            FrameNumber.Items.Add(fn);
            listofFrames.Add(fn);

            RenderRect = fn.RRenderRect;
            CollisionRect = fn.RCollisionRect;
            ActiveRect = fn.RActiveRect;

            FrameNumber.SelectedItem = fn;
            numFrames++;

            if (AnimationFrameNumber.SelectedIndex >= 0)
                AnimationFrameNumber.SetSelected(AnimationFrameNumber.SelectedIndex, false);
            //RenderRect = fn.RRenderRect;
            //CollisionRect = fn.RCollisionRect;
            //ActiveRect = fn.RActiveRect;
        }


        //Bug remove an item
        private void DeleteFrame_Click(object sender, EventArgs e)
        {
            //if (FrameNumber.Items.Contains(FrameNumber.SelectedItem))
            //{
            //    FrameNumber.Items.Remove(FrameNumber.SelectedItem);
            //    numFrames--;
            //}
            //listofFrames.RemoveAt(FrameNumber.SelectedIndex);

            //FrameNumber.Items.RemoveAt(FrameNumber.SelectedIndex);
            
        }

        private void FrameNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listofFrames.Count != 0 && _bIsThereAnimation==false)
            {
                //Frame fn = listofFrames[FrameNumber.SelectedIndex];

                RenderRectXUD.Value = listofFrames[FrameNumber.SelectedIndex].RRenderRect.X;
                RenderRectYUD.Value = listofFrames[FrameNumber.SelectedIndex].RRenderRect.Y;
                RenderRectWUD.Value = listofFrames[FrameNumber.SelectedIndex].RRenderRect.Width;
                RenderRectHUD.Value = listofFrames[FrameNumber.SelectedIndex].RRenderRect.Height;

                CollisionRectXUD.Value = listofFrames[FrameNumber.SelectedIndex].RCollisionRect.X;
                CollisionRectYUD.Value = listofFrames[FrameNumber.SelectedIndex].RCollisionRect.Y;
                CollisionRectWUD.Value = listofFrames[FrameNumber.SelectedIndex].RCollisionRect.Width;
                CollisionRectHUD.Value = listofFrames[FrameNumber.SelectedIndex].RCollisionRect.Height;

                ActiveRectXUD.Value = listofFrames[FrameNumber.SelectedIndex].RActiveRect.X;
                ActiveRectYUD.Value = listofFrames[FrameNumber.SelectedIndex].RActiveRect.Y;
                ActiveRectWUD.Value = listofFrames[FrameNumber.SelectedIndex].RActiveRect.Width;
                ActiveRectHUD.Value = listofFrames[FrameNumber.SelectedIndex].RActiveRect.Height;
            }
        }

        private void MainScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            looping = false;
        }


        void DrawRectangle(Rectangle rect,int _rrect, int _crect, int _arect,string _number)
        {
            ManagedDirect3D.Instance.DrawText(_number, rect.Left - 10, rect.Top, 0, 0, 0);
            ManagedDirect3D.Instance.DrawLine(rect.Left, rect.Top, rect.Left, rect.Bottom, _rrect, _crect, _arect);
            ManagedDirect3D.Instance.DrawLine(rect.Left, rect.Top, rect.Right, rect.Top, _rrect, _crect, _arect);
            ManagedDirect3D.Instance.DrawLine(rect.Left, rect.Bottom, rect.Right, rect.Bottom, _rrect, _crect, _arect);
            ManagedDirect3D.Instance.DrawLine(rect.Right, rect.Top, rect.Right, rect.Bottom, _rrect, _crect, _arect);
        }

        void DrawAnchorPoint(Point ap)
        {
            if (ap.X != 0 && ap.Y != 0)
            {
                ManagedDirect3D.Instance.DrawLine(ap.X - 5, ap.Y - 5, ap.X + 5, ap.Y + 5, 0, 255, 0);
                ManagedDirect3D.Instance.DrawLine(ap.X - 5, ap.Y + 5, ap.X + 5, ap.Y - 5, 0, 255, 0);
            }
        }

        private void RenderRectXUD_ValueChanged(object sender, EventArgs e)
        {
            RenderRect.X = (int)RenderRectXUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex>=0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RRenderRect = new Rectangle(RenderRect.X, fn.RRenderRect.Y, fn.RRenderRect.Width, fn.RRenderRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RRenderRect = new Rectangle(RenderRect.X, fn.RRenderRect.Y, fn.RRenderRect.Width, fn.RRenderRect.Height);
            }
            //Frame fn = listofFrames[FrameNumber.SelectedIndex];
            //fn.RRenderRect = new Rectangle(RenderRect.X,fn.RRenderRect.Y,fn.RRenderRect.Width,fn.RRenderRect.Height);
        }

        private void RenderRectYUD_ValueChanged(object sender, EventArgs e)
        {
            RenderRect.Y = (int)RenderRectYUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RRenderRect = new Rectangle(fn.RRenderRect.X, RenderRect.Y, fn.RRenderRect.Width, fn.RRenderRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RRenderRect = new Rectangle(fn.RRenderRect.X, RenderRect.Y, fn.RRenderRect.Width, fn.RRenderRect.Height);
            }
        }

        private void RenderRectWUD_ValueChanged(object sender, EventArgs e)
        {
            RenderRect.Width = (int)RenderRectWUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RRenderRect = new Rectangle(fn.RRenderRect.X, fn.RRenderRect.Y, RenderRect.Width, fn.RRenderRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RRenderRect = new Rectangle(fn.RRenderRect.X, fn.RRenderRect.Y, RenderRect.Width, fn.RRenderRect.Height);
            }
            
        }

        private void RenderRectHUD_ValueChanged(object sender, EventArgs e)
        {
            RenderRect.Height = (int)RenderRectHUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RRenderRect = new Rectangle(fn.RRenderRect.X, fn.RRenderRect.Y, fn.RRenderRect.Width, RenderRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RRenderRect = new Rectangle(fn.RRenderRect.X, fn.RRenderRect.Y, fn.RRenderRect.Width, RenderRect.Height);
            }
            
        }

        private void CollisionRectXUD_ValueChanged(object sender, EventArgs e)
        {
            CollisionRect.X = (int)CollisionRectXUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RCollisionRect = new Rectangle(CollisionRect.X, fn.RCollisionRect.Y, fn.RCollisionRect.Width, fn.RCollisionRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RCollisionRect = new Rectangle(CollisionRect.X, fn.RCollisionRect.Y, fn.RCollisionRect.Width, fn.RCollisionRect.Height);
            }
            
        }

        private void CollisionRectYUD_ValueChanged(object sender, EventArgs e)
        {
            CollisionRect.Y = (int)CollisionRectYUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RCollisionRect = new Rectangle(fn.RCollisionRect.X, CollisionRect.Y, fn.RCollisionRect.Width, fn.RCollisionRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RCollisionRect = new Rectangle(fn.RCollisionRect.X, CollisionRect.Y, fn.RCollisionRect.Width, fn.RCollisionRect.Height);
            }
            
        }

        private void CollisionRectWUD_ValueChanged(object sender, EventArgs e)
        {
            CollisionRect.Width = (int)CollisionRectWUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RCollisionRect = new Rectangle(fn.RCollisionRect.X, fn.RCollisionRect.Y, CollisionRect.Width, fn.RCollisionRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RCollisionRect = new Rectangle(fn.RCollisionRect.X, fn.RCollisionRect.Y, CollisionRect.Width, fn.RCollisionRect.Height);
            }
            
        }

        private void CollisionRectHUD_ValueChanged(object sender, EventArgs e)
        {
            CollisionRect.Height = (int)CollisionRectHUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RCollisionRect = new Rectangle(fn.RCollisionRect.X, fn.RCollisionRect.Y, fn.RCollisionRect.Width, CollisionRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RCollisionRect = new Rectangle(fn.RCollisionRect.X, fn.RCollisionRect.Y, fn.RCollisionRect.Width, CollisionRect.Height);
            }
            
        }

        private void ActiveRectXUD_ValueChanged(object sender, EventArgs e)
        {
            ActiveRect.X = (int)ActiveRectXUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RActiveRect = new Rectangle(ActiveRect.X, fn.RActiveRect.Y, fn.RActiveRect.Width, fn.RActiveRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RActiveRect = new Rectangle(ActiveRect.X, fn.RActiveRect.Y, fn.RActiveRect.Width, fn.RActiveRect.Height); 
            }
                      
        }

        private void ActiveRectYUD_ValueChanged(object sender, EventArgs e)
        {
            ActiveRect.Y = (int)ActiveRectYUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RActiveRect = new Rectangle(fn.RActiveRect.X, ActiveRect.Y, fn.RActiveRect.Width, fn.RActiveRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RActiveRect = new Rectangle(fn.RActiveRect.X, ActiveRect.Y, fn.RActiveRect.Width, fn.RActiveRect.Height);
            }
            
        }

        private void ActiveRectWUD_ValueChanged(object sender, EventArgs e)
        {
            ActiveRect.Width = (int)ActiveRectWUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RActiveRect = new Rectangle(fn.RActiveRect.X, fn.RActiveRect.Y, ActiveRect.Width, fn.RActiveRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RActiveRect = new Rectangle(fn.RActiveRect.X, fn.RActiveRect.Y, ActiveRect.Width, fn.RActiveRect.Height);
            }
            
        }

        private void ActiveRectHUD_ValueChanged(object sender, EventArgs e)
        {
            ActiveRect.Height = (int)ActiveRectHUD.Value;
            if (_bIsThereAnimation && AnimationFrameNumber.SelectedIndex >= 0)
            {
                Frame fn = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex];
                fn.RActiveRect = new Rectangle(fn.RActiveRect.X, fn.RActiveRect.Y, fn.RActiveRect.Width, ActiveRect.Height);
            }
            else
            {
                Frame fn = listofFrames[FrameNumber.SelectedIndex];
                fn.RActiveRect = new Rectangle(fn.RActiveRect.X, fn.RActiveRect.Y, fn.RActiveRect.Width, ActiveRect.Height);
            }
        }

        private void NewAnimation_Click(object sender, EventArgs e)
        {
            //AnimationName an = new AnimationName();
            //an.ShowDialog();
            if (AnimName.Text.Equals("") == true)
            {
                MessageBox.Show("Please enter a name in the box, to create an animation", "Animation Name", MessageBoxButtons.OK);
            }
            else
            {
                _bIsThereAnimation = true; 
                AnimationFrames af = new AnimationFrames();
                af.SAnimName = AnimName.Text;
                af.LAnimationFrameList = listofFrames;
                af.BIsLooping = false;
                AnimationList.Items.Add(af);
                 
                //AnimationFrameNumber.Items.Clear();
                AnimationList.SelectedItem = af;
                listofAnimations.Add(af);
               
                FrameNumber.SetSelected(FrameNumber.SelectedIndex, false);
                for (int x = 0; x < listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList.Count; ++x)
                {
                    AnimationFrameNumber.Items.Add(listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[x].ToString());
                    //if(x==0)
                }
                AnimationFrameNumber.SelectedIndex=0;
            }
        }

        private void AnimationList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listofAnimations.Count != 0)
            {
                if(AnimationFrameNumber.SelectedIndex>=0)
                    AnimationFrameNumber.SetSelected(AnimationFrameNumber.SelectedIndex, false);
                AnimationFrameNumber.Items.Clear();



                //for (int i = 0; i < listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList.Count; i++)
                //{
                    AnimationFrameNumber.Items.Add(listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList);
               // }
                AnimationFrameNumber.SelectedIndex = 0;


            //    //RenderRectXUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.X;
            //    //RenderRectYUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Y;
            //    //RenderRectWUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Width;
            //    //RenderRectHUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Height;

            //    //CollisionRectXUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect.X;
            //    //CollisionRectYUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect.Y;
            //    //CollisionRectWUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect.Width;
            //    //CollisionRectHUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect.Height;

            //    //ActiveRectXUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect.X;
            //    //ActiveRectYUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect.Y;
            //    //ActiveRectWUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect.Width;
            //    //ActiveRectHUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect.Height;

            //    //TimerUD.Value = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].DTimer;
            //    //AddEventText.Text = listofAnimations[AnimationFrameNumber.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].SEventString;
            }
        }

        private void IsItLoopingCB_CheckedChanged(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex >= 0)
            {
                if (listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList.Count != 0)
                {
                    listofAnimations[AnimationList.SelectedIndex].BIsLooping =
                        !listofAnimations[AnimationList.SelectedIndex].BIsLooping;
                }
            }
            else
                MessageBox.Show("Please create an animation first.", "Animation List", MessageBoxButtons.OK);

            
        }

        private void TimerUD_ValueChanged(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex >= 0)
            {
                if (listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList.Count != 0 && AnimationFrameNumber.SelectedIndex >= 0)
                    listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].DTimer = TimerUD.Value;
            }
            else
                MessageBox.Show("Please create an animation first.", "Animation List", MessageBoxButtons.OK);
        }

        private void AddEventB_Click(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex >= 0)
            {
                if (listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList.Count != 0 && AddEventText.Text != "" && AnimationFrameNumber.SelectedIndex >= 0)
                {
                    listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].SEventString = AddEventText.Text;
                }
            }
            else
                MessageBox.Show("Please create an animation first.", "Animation List", MessageBoxButtons.OK);
        }

        private void AnimationFrameNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList.Count != 0 && _bIsThereAnimation==true && AnimationFrameNumber.SelectedIndex>=0)
            {
                RenderRectXUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.X;
                RenderRectYUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Y;
                RenderRectWUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Width;
                RenderRectHUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RRenderRect.Height;

                CollisionRectXUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect.X;
                CollisionRectYUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect.Y;
                CollisionRectWUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect.Width;
                CollisionRectHUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RCollisionRect.Height;

                ActiveRectXUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect.X;
                ActiveRectYUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect.Y;
                ActiveRectWUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect.Width;
                ActiveRectHUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].RActiveRect.Height;

                TimerUD.Value = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].DTimer;
                AddEventText.Text = listofAnimations[AnimationList.SelectedIndex].LAnimationFrameList[AnimationFrameNumber.SelectedIndex].SEventString;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            looping = false;
            this.Close();
        }

        private void loadImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = RelPath;

            dlg.Filter = "All Files|*.*|BMP files|*.bmp|JPG FIles|*.jpg";

            if (DialogResult.OK == dlg.ShowDialog())
            {

                bmp = ManagedTextureManager.Instance.LoadTexture(dlg.FileName, Color.White.ToArgb());
                     if(AnimationList.SelectedIndex>=0)
                        listofAnimations[AnimationList.SelectedIndex].SImageName = dlg.FileName;
                 currentImageName = dlg.FileName;
            }
        }

        private void loadXMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = RelPath;
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                  listofAnimations.Clear();

                  XElement xRoot = XElement.Load(dlg.FileName);

                  //XElement xAnimationList = xRoot.Element("AnimationList");
                  IEnumerable<XElement> xAnimationList = xRoot.Elements("Animation");
                    

                  foreach (XElement xAnimation in xAnimationList)
                  {
                      AnimationFrames af = new AnimationFrames();
                      af.SAnimName = (string)xAnimation.Attribute("Name");
                      af.BIsLooping = (bool)xAnimation.Attribute("Looping");
                      af.LAnimationFrameList = new List<Frame>();

                      IEnumerable<XElement> xFrames = xAnimation.Elements("Frame");
                      foreach (XElement xFrame in xFrames)
                      {
                          Frame fr = new Frame();
                          fr.FrameNumber = (decimal)xFrame.Attribute("Number");
                          fr.DTimer = (decimal)xFrame.Attribute("Timer");
                          fr.SEventString = (string)xFrame.Attribute("Event");

                          fr.RRenderRect = new Rectangle((int)xFrame.Element("RenderRect").Attribute("X"),
                                                            (int)xFrame.Element("RenderRect").Attribute("Y"),
                                                            (int)xFrame.Element("RenderRect").Attribute("Width"),
                                                            (int)xFrame.Element("RenderRect").Attribute("Height"));
                          fr.RCollisionRect = new Rectangle((int)xFrame.Element("CollisionRect").Attribute("X"),
                                                            (int)xFrame.Element("CollisionRect").Attribute("Y"),
                                                            (int)xFrame.Element("CollisionRect").Attribute("Width"),
                                                            (int)xFrame.Element("CollisionRect").Attribute("Height"));
                          fr.RActiveRect = new Rectangle((int)xFrame.Element("ActiveRect").Attribute("X"),
                                                            (int)xFrame.Element("ActiveRect").Attribute("Y"),
                                                            (int)xFrame.Element("ActiveRect").Attribute("Width"),
                                                            (int)xFrame.Element("ActiveRect").Attribute("Height"));
                          fr.PAnchorPoint = new Point((int)xFrame.Element("AnchorPoint").Attribute("X"),
                                                            (int)xFrame.Element("AnchorPoint").Attribute("Y"));

                          af.LAnimationFrameList.Add(fr);
                      }

                      listofAnimations.Add(af);
                  }
                }

            foreach (AnimationFrames af in listofAnimations)
            {
                AnimationList.Items.Add(af);
                //AnimationList.Items.ToString();
                AnimationList.SelectedIndex = 0;
                foreach (Frame fr in af.LAnimationFrameList)
                {
                    AnimationFrameNumber.Items.Add(fr);
                }
            }

            
        }

        private void saveToXMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listofAnimations.Count == 0)
            {
                MessageBox.Show("Please create an animation first.", "Animation List", MessageBoxButtons.OK);
            }
            else
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.InitialDirectory = RelPath;
                dlg.Filter = "All Files|*.*|Xml Files|*.xml";
                dlg.FilterIndex = 2;
                dlg.DefaultExt = "xml";
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    XElement xRoot = new XElement("AnimationList");

                    foreach (AnimationFrames af in listofAnimations)
                    {
                        XElement xAnimation = new XElement("Animation");
                        xAnimation.SetAttributeValue("Looping", af.BIsLooping.ToString());
                        xAnimation.SetAttributeValue("Name", af.SAnimName.ToString());
                        xRoot.Add(xAnimation);

                        foreach (Frame fr in af.LAnimationFrameList)
                        {
                            XElement xFrame = new XElement("Frame");
                            xFrame.SetAttributeValue("Event", fr.SEventString.ToString());
                            xFrame.SetAttributeValue("Timer", fr.DTimer);
                            xFrame.SetAttributeValue("Number", fr.FrameNumber);
                            xAnimation.Add(xFrame);

                            XElement xRREct = new XElement("RenderRect");
                            xRREct.SetAttributeValue("Height", fr.RRenderRect.Height);
                            xRREct.SetAttributeValue("Width", fr.RRenderRect.Width);
                            xRREct.SetAttributeValue("Y", fr.RRenderRect.Y);
                            xRREct.SetAttributeValue("X", fr.RRenderRect.X);
                            xFrame.Add(xRREct);

                            XElement xCREct = new XElement("CollisionRect");
                            xCREct.SetAttributeValue("Height", fr.RCollisionRect.Height);
                            xCREct.SetAttributeValue("Width", fr.RCollisionRect.Width);
                            xCREct.SetAttributeValue("Y", fr.RCollisionRect.Y);
                            xCREct.SetAttributeValue("X", fr.RCollisionRect.X);
                            xFrame.Add(xCREct);

                            XElement xAREct = new XElement("ActiveRect");
                            xAREct.SetAttributeValue("Height", fr.RActiveRect.Height);
                            xAREct.SetAttributeValue("Width", fr.RActiveRect.Width);
                            xAREct.SetAttributeValue("Y", fr.RActiveRect.Y);
                            xAREct.SetAttributeValue("X", fr.RActiveRect.X);
                            xFrame.Add(xAREct);

                            XElement xAPOint = new XElement("AnchorPoint");
                            xAPOint.SetAttributeValue("X", fr.PAnchorPoint.X);
                            xAPOint.SetAttributeValue("Y", fr.PAnchorPoint.Y);
                            xFrame.Add(xAPOint);
                        }
                    }
                    xRoot.Save(dlg.FileName);
                }
            }
            
        }

        private void splitContainer2_Panel2_MouseClick(object sender, MouseEventArgs e)
        {
            if (_bZoom)
            {
                if (e.Button == MouseButtons.Left)
                {
                   // ManagedDirect3D.Instance.ChangeDisplayParam(splitContainer2.Panel2, splitContainer2.Panel2.Width+=100, splitContainer2.Panel2.Height+=100, true, false);
                }
                else if (e.Button == MouseButtons.Right)
                {

                }
            }
        }

        private void PlayB_Click(object sender, EventArgs e)
        {
            _bPlay = true;
            _bPause = false;
            _bStop = false;
            

        }

        private void PauseB_Click(object sender, EventArgs e)
        {
            _bPlay = false;
            _bPause = true;
            _bStop = false;
        }

        private void StopB_Click(object sender, EventArgs e)
        {
            _bPlay = false;
            _bPause = false;
            _bStop = true;
            if(AnimationFrameNumber.SelectedIndex>=0)
                 AnimationFrameNumber.SelectedIndex = 0;
        }

        private void loadFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = RelPath;

            dlg.Filter = "All Files|*.*|BMP files|*.bmp|JPG FIles|*.jpg";

            if (DialogResult.OK == dlg.ShowDialog())
            {

                bkg = ManagedTextureManager.Instance.LoadTexture(dlg.FileName, Color.White.ToArgb());

            }
        }













  
 


        























    }
}
